﻿using System;
using System.Collections.Generic;
using System.Data;
using MedicService.Domain;
using MedicService.Server.Factory.Generic;

namespace MedicService.Server.Factory {
    public class UserFactory : EntityFactory<User> {
        public UserFactory() : base("User", "login") {}
        protected override User CreateEntity() {
            return new User(){LastActivity = System.DateTime.Now};
        }

        protected override string GetInsertQuery(User entity) {
            entity.LastActivity = System.DateTime.Now;
            string q = "INSERT INTO [User]"
                       + " ([login]"
                       + " ,[password]"
                       + " ,[role]"
                       + " ,[sessionId]"
                       + " ,[lastActivity])"
                       + " VALUES"
                       + " ('"+entity.Login+"'"
                       + " ,'"+entity.Password+"'"
                       + " ,'"+entity.Role+"'"
                       + " ,'"+entity.SessionId+"'"
                       + " ,"+Date(entity.LastActivity)+");"
                       + " ";
            return q;
        }

        protected override string GetUpdateQuery(User entity) {
            string q = "UPDATE [User]"
                       + " SET [login] = '"+entity.Login+"'"
                       + " ,[password] = '" + entity.Password + "'"
                       + " ,[role] = '" + entity.Role + "'"
                       + " ,[sessionId] = '" + entity.SessionId + "'"
                       + " ,[lastActivity] = " + DateTime(entity.LastActivity)
                       + " WHERE id=" + entity.Id;
            return q;
        }

        protected override void FillEntity(User entity, DataRow myDataRow) {
            base.FillEntity(entity, myDataRow);
            entity.LastActivity = (DateTime) myDataRow["lastActivity"];
            entity.Login = (string) myDataRow["login"];
            entity.Password = (string)myDataRow["password"];
            entity.Role = (string)myDataRow["role"];
            entity.SessionId = (string)myDataRow["sessionId"];
        }

        public List<User> GetList(string login, string password) {
            var entities = new List<User>();
            var dataTable = DatabaseConnector.GetData(TableName, OrderBy, " login='" + login + "' AND password='" + password + "'");
            foreach (DataRow myDataRow in dataTable.Rows) {
                var entity = CreateEntity();
                FillEntity(entity, myDataRow);
                entities.Add(entity);
            }
            return entities;
        }

        public List<User> GetList(string sessionId) {
            var entities = new List<User>();
            var dataTable = DatabaseConnector.GetData(TableName, OrderBy, " sessionId='" + sessionId + "'");
            foreach (DataRow myDataRow in dataTable.Rows) {
                var entity = CreateEntity();
                FillEntity(entity, myDataRow);
                entities.Add(entity);
            }
            return entities;
        }

        public void SavePublic(PublicUser entity) {
            if (entity.Id != 0) {
                string update = "UPDATE [User]";
                update += " SET [login] = '" + entity.Login + "'";
                if (!string.IsNullOrEmpty(entity.Password)) {
                    update += " ,[password] = '" + entity.Password + "'";
                }
                update += " ,[role] = '" + entity.Role + "'";
                update += " WHERE id=" + entity.Id;

                DatabaseConnector.Update(update);
            } else {
                var user = new User();
                user.Login = entity.Login;
                user.Password = entity.Password;
                user.Role = entity.Role;
                user.SessionId = "";
                Save(user);
                entity.Id = user.Id;
            }
        }

        public List<PublicUser> GetPublicList(bool useDeleted) {
            List<User> users = GetList(useDeleted);
            List<PublicUser> publicUsers = new List<PublicUser>();
            foreach (var user in users) {
                var publicUser = new PublicUser();
                publicUser.Id = user.Id;
                publicUser.Login = user.Login;
                publicUser.Role = user.Role;
                publicUsers.Add(publicUser);
            }
            return publicUsers;
        }

        public PublicUser LoadPublic(int id) {
            var publicUser = new PublicUser();
            var user = Load(id);
            publicUser.Id = user.Id;
            publicUser.Login = user.Login;
            publicUser.Role = user.Role;
            return publicUser;
        }
    }
}